package com.zhangsun.tools;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.zhangsun.bean.PreProject;
import com.zhangsun.bean.Project;
import net.sf.mpxj.*;
import net.sf.mpxj.mpp.MPPReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class MmpHelper {

    public List<Project> readMmpFileToDB(File file) {
        try {
            MPPReader mppRead = new MPPReader();
            ProjectFile pf = mppRead.read(file);
            System.out.println(file.getName());

            List<Task> tasks = pf.getChildTasks();
            System.out.println("tasks.size() : " + tasks.size());
            List<Project> proList = new LinkedList<Project>();

            // Project pro = new Project();
            // pro.setBatchNum(IdUtil.randomUUID());//生成批次号UUID

            List<Task> children = tasks.get(0).getChildTasks();
            for (Task value : children) {
                getChildrenTask2(value, null, proList, 0);
            }
            for (Project project : proList) {
                if (project.getPreProjects() != null && project.getPreProjects().size() > 0) {
                    for (PreProject preProject : project.getPreProjects()) {
                        preProject.setTaskId(findIdByTaskId(proList, preProject.getTaskId()));
                    }
                }
            }
            return proList;
        } catch (MPXJException e) {
            StaticLog.error(e.getMessage());
            throw new RuntimeException();
        } catch (Exception e) {
            StaticLog.error(e.getMessage());
            throw new RuntimeException();
        }
    }

    private void getChildrenTask2(Task task, String parentId, List<Project> list, int levelNum) {
        Project project = new Project();
        project.setId(IdUtil.randomUUID());
        project.setParentId(parentId);//将上一级目录的Id赋值给下一级
        project.setLevel(levelNum);
        project.setTaskId(task.getID().toString());
        project.setTaskName(task.getName());
        project.setDurationDate(task.getDuration().toString());
        project.setStartDate(task.getStart());
        project.setEndDate(task.getFinish());
        project.setImportTime(new Date());
        project.setResource(task.getResourceGroup());

        project.setText1(task.getText(1));
        project.setText2(task.getText(2));
        project.setText3(task.getText(3));
        project.setText4(task.getText(4));

        project.setNotes(task.getNotes());

        // 前置任务
        List<Relation> predecessors = task.getPredecessors();
        if (predecessors != null && predecessors.size() > 0) {
            List<PreProject> preProjects = new ArrayList<>();
            for (Relation relation : predecessors) {
                PreProject preProject = new PreProject();

                Task targetTask = relation.getTargetTask();
                preProject.setTaskId(targetTask.getID().toString());
                preProject.setTaskName(targetTask.getName());

                preProject.setType(relation.getType().toString());
                preProject.setLag(relation.getLag().toString());
                preProjects.add(preProject);
            }
            project.setPreProjects(preProjects);
        }
        list.add(project);

        List<Task> tasks = task.getChildTasks();
        if (tasks.size() > 0) {
            levelNum++;
            for (Task value : tasks) {
                getChildrenTask2(value, project.getId(), list, levelNum);
            }
        }
    }

    /*private void getChildrenTask(Task task, Project project, List<Project> list, int levelNum) {
        if (task.getResourceAssignments().size() == 0) {
            levelNum++;//层级号需要增加
            List<Task> tasks = task.getChildTasks();
            for (Task value : tasks) {
                if (value.getResourceAssignments().size() == 0) {//说明还是在父任务层
                    System.out.println("+++++" + value);
                    Project pro = new Project();
                    if (StrUtil.isNotBlank(project.getProjId())) {//说明不是第一次读取了
                        pro.setParentId(project.getProjId());//将上一级目录的Id赋值给下一级
                    }
                    pro.setBatchNum(project.getBatchNum());
                    pro.setImportTime(new Date());
                    pro.setLevel(levelNum);
                    pro.setTaskName(value.getName());
                    pro.setDurationDate(value.getDuration().toString());
                    pro.setStartDate(value.getStart());
                    pro.setEndDate(value.getFinish());
                    pro.setResource(value.getResourceGroup());

                    // 前置任务
                    List<Relation> predecessors = task.getPredecessors();
                    if (predecessors != null && predecessors.size() > 0) {
                        for (Relation relation : predecessors) {
                            PreProject preProject = new PreProject();

                            Task targetTask = relation.getTargetTask();
                            preProject.setTaskId(targetTask.getID().toString());
                            preProject.setTaskName(targetTask.getName());

                            preProject.setType(relation.getType().toString());
                            preProject.setLag(relation.getLag().toString());
                            pro.setPreProject(preProject);
                        }
                    }

                    pro.setProjId(IdUtil.randomUUID());
                    list.add(pro);
                    getChildrenTask(value, pro, list, levelNum);
                } else {
                    getChildrenTask(value, project, list, levelNum);
                }
            }
        } else {
            getResourceAssignment(task, project, list, levelNum);
        }
    }

    private void getResourceAssignment(Task task, Project project, List<Project> proList, int levelNum) {
        List<ResourceAssignment> list = task.getResourceAssignments();
        ResourceAssignment rs = list.get(0);
        System.out.println("task = [" + task.getName());
        Project pro = new Project();
        pro.setTaskName(task.getName());
        pro.setParentId(project.getProjId());
        pro.setLevel(levelNum);
        pro.setImportTime(new Date());
        pro.setBatchNum(project.getBatchNum());
        pro.setDurationDate(task.getDuration().toString());
        pro.setStartDate(rs.getStart());
        pro.setEndDate(rs.getFinish());
        StringBuilder resource = new StringBuilder();
        if (list.size() > 1) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getResource() != null) {
                    if (i < list.size() - 1) {
                        resource.append(list.get(i).getResource().getName()).append(",");
                    } else {
                        resource.append(list.get(i).getResource().getName());
                    }
                }
            }
        } else {

            if (list.size() > 0 && list.get(0).getResource() != null) {
                resource = new StringBuilder(list.get(0).getResource().getName());
            }
        }
        if (!StrUtil.isEmpty(resource.toString())) {
            pro.setResource(resource.toString());
        }

        // 前置任务
        List<Relation> predecessors = task.getPredecessors();
        if (predecessors != null && predecessors.size() > 0) {
            for (Relation relation : predecessors) {
                PreProject preProject = new PreProject();

                Task targetTask = relation.getTargetTask();
                preProject.setTaskId(targetTask.getID().toString());
                preProject.setTaskName(targetTask.getName());

                preProject.setType(relation.getType().toString());
                preProject.setLag(relation.getLag().toString());

                pro.setPreProject(preProject);
            }
        }

        pro.setProjId(IdUtil.randomUUID());
        proList.add(pro);
    }*/

    private String findIdByTaskId(List<Project> tasks, String taskId) {
        String id = null;
        for (Project project : tasks) {
            if (taskId.equals(project.getTaskId())) {
                id = project.getId();
                break;
            }
        }
        return id;
    }

}
